#include "appBll.h"
#include "MHD_utils.h"
#define PAGE_BUFFER_SIZE  4096
#define MAX_STR_SIZE 16
extern mr_log_p MHD_log;
extern mr_conf_p pconf;
#include "handlers.c"

int sizeof_handler_table(void)
{
	return ARRAY_SIZE(handler_table);
}

int appcon_new(app_con_info_p *pappcon){
	
	app_con_info_p myappcon;
	myappcon = (app_con_info_p)malloc(sizeof(app_con_info_t));
	memset(myappcon,0,sizeof(app_con_info_t));
	*pappcon =myappcon;
	return 0;
}




int handle_main_request(app_con_info_p pappcon,mr_log_p plog,mr_conf_p pconf){

	char uri_node1[MAX_URL_LEN] = {0};
	char uri_node2[MAX_URL_LEN] = {0};
char handler_name[MAX_URL_LEN]={0};
	int i, pos, uri_num1=0;	
	pappcon->request_mime_type = MHD_lookup_connection_value(pappcon->connection
			,MHD_HEADER_KIND, "Content-Type");
	mr_logger_write(plog,LL_TRACE,"url : %s\n",pappcon->url);
	mr_logger_write(plog,LL_TRACE,"method : %s\n",pappcon->method);
	mr_logger_write(plog,LL_TRACE,"post_data : %s\n",pappcon->post_data);
	mr_logger_write(plog,LL_TRACE,"request_mime_type : %s\n",pappcon->request_mime_type);

	pos = parse_url_node(pappcon->url, uri_node1, 0);
  if (pos <=0)

 return  response_write(pappcon->connection,"text/html",MHD_HTTP_INTERNAL_SERVER_ERROR
			,"Could not parse first element of URL: %s\n",pappcon->url);
 

  pos = parse_url_node(pappcon->url, uri_node2, pos);
  if (pos < 0)

 return  response_write(pappcon->connection,"text/html",MHD_HTTP_INTERNAL_SERVER_ERROR
			,"Could not parse second element of URL: %s\n",pappcon->url);
 
  if (pos) {
    if (isdigit(uri_node2[0])) {
      uri_num1=atoi(uri_node2);
      uri_node2[0]='\0';
      /* 2nd element was a number, get next node */
      pos = parse_url_node(pappcon->url, uri_node2, pos);
      if (pos < 0)
	 return  response_write(pappcon->connection,"text/html",MHD_HTTP_INTERNAL_SERVER_ERROR
			,"Could not parse third element of URL: %s\n",pappcon->url);
 
    }
  }
    
  strcat(handler_name, uri_node1);
  if (uri_num1)
    strcat(handler_name, "/#");
  if (strlen(uri_node2)) {
    strcat(handler_name, "/");
    strcat(handler_name, uri_node2);
  }	

	for(i=0; i<sizeof_handler_table(); i++) {
		mr_logger_write(plog,LL_TRACE,"handlaer table .name : %s : handler name  : %s\n", handler_table[i].name, handler_name);
		if (!strcmp(handler_table[i].name, handler_name)) {
			
			if (!strcmp(pappcon->method, "POST")) 
			{
				mr_logger_write(plog,LL_TRACE,"post handler : %s\n"
						,handler_table[i].post_handler);
				if ( handler_table[i].post_handler )
					return handler_table[i].post_handler(
							pappcon->connection,
							pappcon,i);
				else
					return response_write (pappcon->connection, pappcon->request_mime_type, MHD_HTTP_NOT_FOUND,
								"Unsupported POST request \n");
			}
     
			if(!strcmp(pappcon->method,"GET"))
			{
				mr_logger_write(plog,LL_TRACE,"get  handler : %s\n",
				handler_table[i].get_handler);
				if ( handler_table[i].get_handler )
					return handler_table[i].get_handler(pappcon->connection, pappcon,uri_num1);
				else
					return response_write (pappcon->connection, pappcon->request_mime_type, MHD_HTTP_NOT_FOUND,
							                 "Unsupported GET request \n ");

		
			} 
		      break;
		}
	}
	if (i==sizeof_handler_table())
	    return response_write (pappcon->connection, pappcon->request_mime_type, MHD_HTTP_NOT_FOUND,
			                       "Unsupported request\n ");

	return 0;
}


int list_all_nodes(struct MHD_Connection *connection,app_con_info_p sysdata, int uri_num){

	struct MHD_Response *response;
	int ret;
	const char *page = "<html><body>Unsupported request</body></html>";
	
	mr_logger_write(MHD_log,LL_TRACE,"list_all_node\n");
        
	if(page)
        {
		 mr_logger_write(MHD_log,LL_TRACE,"page\n");
		 response =MHD_create_response_from_buffer (strlen (page),
			       (void *) page,MHD_RESPMEM_PERSISTENT);
        }
        if (!response)
                 return MHD_NO;

        ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
        MHD_destroy_response (response);
        return ret;
}
int list_a_node(struct MHD_Connection *connection,app_con_info_p sysdata, int uri_num){
int ret;	
	mr_logger_write(MHD_log,LL_TRACE,"list_a_node\n");
     return response_write(connection,"application/json",MHD_HTTP_OK,"get ok");    

        return ret;

}
int create_a_node(struct MHD_Connection *connection,app_con_info_p sysdata, int uri_num){

//	json_t *root;
//	json_error_t error;
	//json_t *commits;	
	//size_t i;	

	mr_logger_write(MHD_log,LL_TRACE,"create_a_node\n");
	mr_logger_write(MHD_log,LL_TRACE,"url : %s\n",sysdata->url);
	mr_logger_write(MHD_log,LL_TRACE,"method : %s\n",sysdata->method);
	mr_logger_write(MHD_log,LL_TRACE,"post_data : %s\n",sysdata->post_data);
	mr_logger_write(MHD_log,LL_TRACE,"request_mime_type : %s\n",sysdata->request_mime_type);
	
/*
	if(sysdata->post_data){
		root = json_loads(sysdata->post_data,0,&error);	
		if(!root)
		{
			mr_logger_write(plog,LL_TRACE,"error : %s at line %d\n",error.text,error.line);
			return present_response(connection,"application/json",MHD_HTTP_INTERNAL_SERVER_ERROR,"error :%s at line %d\n",error.text,error.line);	
		}
		if(!json_is_array(root))
		    {
			json_decref(root);
			return	present_response(connection,"application/json",MHD_HTTP_INTERNAL_SERVER_ERROR,"   {\"error\" :\" root is not an array\" }\n");	
		    }
		for(i = 0; i < json_array_size(root); i++) {
			json_t *data, *sha ;
		//	 char *message_text;
			data = json_array_get(root, i);
			if(!json_is_object(data))
			{
				return	present_response(connection,"application/json",MHD_HTTP_INTERNAL_SERVER_ERROR,"error: commit data %d is not an object\n",(int)(i + 1));
				json_decref(root);
			}

			sha = json_object_get(data, "sha");
			if(!json_is_string(sha))
			{
				return	present_response(connection,"application/json",MHD_HTTP_INTERNAL_SERVER_ERROR,"error: commit %d: sha is not a string\n",(int)(i + 1));
			}

		//	message_text = json_string_value(message);
		}

		return	present_response(connection," application/json; charset=utf-8",MHD_HTTP_OK,"{\"result\" : \"Success\"} \n");
		json_decref(root);

	}// if post data  
 // there is no post data  
	
*/	return	response_write(connection,"text/html",MHD_HTTP_INTERNAL_SERVER_ERROR,"Problem  With Post  there is no post Data\n");
}



int update_a_node(struct MHD_Connection *connection,app_con_info_p sysdata, int uri_num){

	mr_logger_write(MHD_log,LL_TRACE,"update_a_node\n");
	return 0;
}

int remove_a_node(struct MHD_Connection *connection,app_con_info_p sysdata, int uri_num){
	 
	mr_logger_write(MHD_log,LL_TRACE,"remove_a_node\n");
	return 0;

}

